namespace OFX.Aggregator.Implementation
{
	#region #using Directives

	using System;
	using System.Linq;

	using Infrastructure;

	#endregion

	public static class StatementInterpreterHelper
	{
		public static void InferStatementDates(Statement statement)
		{
			foreach (var paymentOriginator in statement.PaymentOriginators)
			{
				if (paymentOriginator.StatementLines.Count > 0)
				{
					var firstLineDate = (from statementLine in paymentOriginator.StatementLines select statementLine.DatePosted).Min();
					var lastLineDate = (from statementLine in paymentOriginator.StatementLines select statementLine.DatePosted).Max();

					if (statement.DateStart == DateTime.MinValue || firstLineDate < statement.DateStart)
					{
						statement.DateStart = firstLineDate;
					}
					if (statement.DateEnd == DateTime.MinValue || lastLineDate > statement.DateEnd)
					{
						statement.DateEnd = lastLineDate;
					}
				}
			}
		}
	}
}